using Dal;
using Domain;
using IDal;
using Infrastructure;
using SqlSugar;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddScoped<IService, Service>();

builder.Services.AddSingleton<ISqlSugarClient>(p => new SqlSugarClient(new ConnectionConfig()
{
    ConnectionString = builder.Configuration.GetConnectionString("DB"),
    DbType = DbType.MySql,
    IsAutoCloseConnection=true
}));
builder.Services.AddScoped<DapperHelper>();

builder.Services.AddSingleton<Infrastructure.DbContext>();

var app = builder.Build();

// 自动建表（CodeFirst）
using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>();
    db.CodeFirst.InitTables
        (
        typeof(T_User),
        typeof(RoleMOdel),
        typeof(Mapp)
        ); // 自动映射T_User表(首次会创建)这块代码可以封装到一个扩展类中
}

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();


